import * as appConfig from '../../../app-config.js';
var app = getApp()
Page({
  data: {
    topCurrentId:'1',
    leftCurrentId:'',
    leftBtn:[],
    topBtn:[],
    list:[],
    backBox:[],
    parent_id:0,
    goodsType:'',
    addImg:'../../../image/add.png',
    priceName:'进货价',
    retailPrice:true,
    agentId:'',
    page:2,
    noData:false,
    noType:false,
    isMask:false,
    inviteCode:'',
    isVisitor:'',
    accountId:'',
    banner:'',
    parentType:1000,
    isCompany:true,
    settlementCount:0,
    togetherType:0
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        accountId:res.data.id,
        agentId:res.data.agentId
      })
      this.setData({
        agentId:res.data.agentId
      })
      this.getFirstTypeList(res.data.agentId)
        // this.getGoodsTypeList(res.data.agentId)
      this.getGoodsTypeList(res.data.agentId,0,0)
      this.getSettlementCount(res.data.agentId,res.data.id)
    })
    this.banner()

  },
  onShow(){
    app.getMemInfo().then(res=>{
      this.getSettlementCount(res.data.agentId,res.data.id)
    })
  },
  // bindLeftBtn(e){
  //   let type = e.currentTarget.dataset.type
  //   let parent = e.currentTarget.dataset.parent
  //   this.setData({
  //     leftCurrentId:e.currentTarget.dataset.id,
  //     goodsType:type,
  //     parentType:parent,
  //     page:2
  //   })
  //   this.getGoodsList()
  // },
  bindToSettlement(){
    wx.navigateTo({
      url: 'purchaseSettlement'
    })
  },
  bindToDetail(e){
    let buyType = 0
    if (this.data.topCurrentId==0) {//0拼单 1批发
      buyType = 0
    }else{
      buyType = 1
    }
    wx.navigateTo({
      url:'purchaseDetail?type=2&goodsType='+this.data.goodsType+
          '&id='+e.currentTarget.dataset.id+
          '&color='+e.currentTarget.dataset.color+
          '&company='+e.currentTarget.dataset.company+
          '&buyType='+buyType
    })
  },
  //获取商品列表
  getGoodsList(){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      list:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      parentType:this.data.parentType,
      pageNum:1,
      searchKey:'',
      togetherType:this.data.togetherType
    }
    let list = []
    console.log(data)
    app.request('wx_app/product/getAllProductList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.product_name,
            price:item.price==null?0:item.price,
            stockPrice:item.stockPrice==null?0:item.stockPrice,
            url:item.main_img_url,
            id:item.id,
            color:item.tag_color,
            sales:item.sales,
            companyName:item.product_tag,
            deliberation:item.deliberation,
            together_price:item.together_price,
            set_price:item.set_price3==null?item.set_price2==null?item.set_price1:item.set_price2:item.set_price3
          })
          that.setData({
            list,
          })
        })
        wx.hideLoading()
        if (that.data.list.length==0) {
          that.setData({
            noData:true
          })
        }else{

          that.setData({
            noData:false
          })
        }
      }else{
        that.setData({
          noData:true
        })
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  bindMore(){
    wx.showLoading({
      title:'正在加载'
    })
    let page = this.data.page
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum:page,
      parentType:this.data.parentType,
      searchKey:'',
      togetherType:this.data.togetherType
    }
    let list = this.data.list
    console.log(data)
    app.request('wx_app/product/getAllProductList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.product_name,
            price:item.price==null?0:item.price,
            stockPrice:item.stockPrice==null?0:item.stockPrice,
            url:item.main_img_url,
            id:item.id,
            color:item.tag_color,
            sales:item.sales,
            companyName:item.product_tag,
            deliberation:item.deliberation,
            set_price:item.set_price3==null?item.set_price2==null?item.set_price1:item.set_price2:item.set_price3
          })
          that.setData({
            list
          })
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
    page++
    this.setData({
      page:page
    })
  },
  //获取一级分类
  getFirstTypeList(agentId){
    const that = this
    let topBtn = this.data.topBtn
    let data = {
      agentId,
      requestType:'agent'
    }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      // topBtn.push({name:"整车类",id:2,parent:0})
      res.data.product_types.forEach(item=>{
        if (item.parent_id==0&&item.id!=98) {
          topBtn.push({name:item.name,id:item.id,parent:item.parent_id})
        }
      })
      that.setData({
        topBtn
      })
    })
  },
  chooseAll(){
    this.setData({
      topCurrentId:'all',
      parent_id:0,
      leftCurrentId:0
    })
    this.getGoodsTypeList(this.data.agentId,0,0)
  },
  //进货首页分类修改 2019/3/20
  // chooseFirstType(e){
  //   let type = e.currentTarget.dataset.type
  //   if (type==0) {
  //     this.setData({
  //       parentType:2,
  //       goodsType:99,
  //       topCurrentId:0,
  //       page:1,
  //     })
  //   }else{
  //     this.setData({
  //       goodsType:'',
  //       parentType:1000,
  //       topCurrentId:1,
  //       page:2,
  //     })
  //   }
  //   this.getGoodsList()
  // },
  chooseFirstType(e){
    let type = e.currentTarget.dataset.type
    if (type==0) {
      this.setData({
        parentType:0,
        goodsType:0,
        topCurrentId:0,
        page:1,
        togetherType:1
      })
    }else{
      this.setData({
        goodsType:0,
        parentType:0,
        topCurrentId:1,
        page:2,
        togetherType:0
      })
    }
    this.getGoodsList()
  },
  //获取商品种类
  getGoodsTypeList(agentId,type,isAll){
    const that = this
    let leftBtn = [],
        backBox = [],
        goodsType='',
        parentType='',
    data = {
      agentId,
      requestType:'agent',
      type:isAll
    }
    console.log(data)
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      res.data.product_types.forEach(item=>{
        if (type==item.parent_id&&type!=0) {
          leftBtn.push({name:item.name,id:item.id,parent:item.parent_id})
        }else if (type==0) {
          if (item.parent_id!=0) {
            leftBtn.push({name:item.name,id:item.id,parent:item.parent_id})
          }
        }
        if (type==item.id) {
          backBox.push({name:item.name,id:item.id,parent:item.parent_id})
        }
      })
      that.setData({
        leftBtn,
        backBox
      })
      if (that.data.leftBtn==''&&that.data.topCurrentId=='all') {
        res.data.product_types.forEach(item=>{
          leftBtn.push({name:item.name,id:item.id})
        })
        that.setData({
          leftBtn
        })
      }
      if (that.data.leftBtn=='') {
        that.setData({
          leftBtn:that.data.backBox
        })
      }
      // goodsType = that.data.leftBtn[0].id
      // parentType = that.data.leftBtn[0].parent
      // that.setData({
      //   goodsType,
      //   parentType
      // })
      that.getGoodsList()
    })
  },
  bindSearch(){
    wx.navigateTo({
      url:'purchaseSearch'
    })
  },
  getInviteCode(e){
    this.setData({
      inviteCode:e.detail.value
    })
  },
  bindCancel(){
    wx.navigateBack({
      delta: 1
    })
  },
  bindConfirm(){
    let that = this
    let data = {
      accountId:this.data.accountId,
      inviteCode:this.data.inviteCode
    }
    app.request('wx_app/agent_account/input_inviteCode',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.setStorage({
          key:'memInfo',
          data:{
            agentId:res.data.agentId,
            id:res.data.id==null?0:res.data.id,
            gender:res.data.gender,
            username:res.data.username,
            name:res.data.name,
            role:res.data.role,
            isEnable:res.data.isEnable,
            shop:res.data.agentCompanyName,
            isVisitor:res.data.isVisitor,
            vipUserName:res.data.vipUserName,
            phone:res.data.phone,
            vipName:res.data.vipName,
            vipId:res.data.vipId
          },
          success(item){
            wx.showToast({
              title:'认证成功'
            })
            that.setData({
              isMask:false
            })
            that.getFirstTypeList(res.data.agentId)
            that.getGoodsTypeList(res.data.agentId,0,1)
          },
          fail(item){
            app.errorMsg(item)
          }
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  banner(){
    let data = {}
    app.request('wx_app/banner',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {

          this.setData({
            banner:res.data[0].imgUrl
          })

      }else{

      }
    })
  },
  //获取购物车数量
  getSettlementCount(agentId,accountId){
    let data = {
      agentId,
      accountId
    }
    console.log(data)
    app.request('wx_app/stock_cart/queryCartListByAgentId',data,'GET').then(res=>{
      console.log(res)
      let count = 0
      res.data.forEach(item=>{
        item.carts.forEach(_item=>{
          count+=_item.count
        })
      })
      this.setData({
        settlementCount:count
      })
    })
  }
})
